using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteECSWeb;
using System.IO;

public partial class Offices_OfficeList : PageBase
{

    #region ResetAdapter
    protected override void OnPreInit(EventArgs e)
    {
        
        HttpContext.Current.Request.Browser.
            Adapters["System.Web.UI.WebControls.GridView, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"]
            = "SiteECSWeb.GridViewAdapterReset";
        base.OnPreInit(e);
    }
    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        HttpContext.Current.Request.Browser.
           Adapters["System.Web.UI.WebControls.GridView, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"]
           = "CSSFriendly.GridViewAdapter";
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        GridViewHelper gHelper = new GridViewHelper(this.GridViewCus1);

        gHelper.RegisterGroup("EcsInternalName", true, true);
        gHelper.GroupHeader += new GroupEvent(gHelper_GroupHeader);

        if (!IsPostBack)
        {
            pnlReportView.Visible = 
                ApplyManageForAuthonticatedUser(User.Identity.Name, 
                RolesName.SysAdmin, RolesName.AdminSystem, RolesName.ECSInternal);
            pnlRepotMob.Visible = ApplyManageForAuthonticatedUser(User.Identity.Name,
                RolesName.SysAdmin);
            hyplnkOffReport.NavigateUrl =
                Helper.NavURLParamsIntVirtualName("WithMob", Globals.Settings.VirtualNamesInfo.OfficeReport);
            hyplnkOffReportNotMob.NavigateUrl =
                Helper.NavURLParamsIntVirtualName("WithOutMob", Globals.Settings.VirtualNamesInfo.OfficeReport);       
            #region SiteMap
            //this.SiteMapNodeProvider = SiteMapProviderNames.DynamicSiteMapProvider.ToString();
            this.SiteMapNodeProvider = SiteMapProviderNames.ListMXmlSiteMapProvider.ToString();
            SiteMapPath1.SiteMapProvider = SiteMapNodeProvider;

            #endregion
                
        }
        
        //gHelper.ApplyGroupSort();
       
        //this.GridViewCus1.Sort("EcsInternalName", 
        //    SortDirection.Ascending);
        //HttpContext.Current.Request.Browser
        //    .Adapters[];
    
    }

    void gHelper_GroupHeader(string groupName, object[] values, GridViewRow row)
    {
        row.CssClass= "groupHead";
        //row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;
    }
    protected override void Render(HtmlTextWriter writer)
    {

        //if (!string.IsNullOrEmpty(this.GridViewCus1.SortExpression))
        //{
        //    int sortColumnIndex = -1;
        //    string sortColumnHeaderText = string.Empty;
        //    for (int i = 0; i < GridViewCus1.Columns.Count; i++)
        //    {
        //        if (GridViewCus1.Columns[i].SortExpression.
        //            CompareTo(GridViewCus1.SortExpression) == 0)
        //        {
        //            sortColumnIndex = i;
        //            sortColumnHeaderText = GridViewCus1.Columns[i].HeaderText;
        //            break;
        //        }
        //    }
        //    Table gridTable = (Table)GridViewCus1.Controls[0];
        //    string lastValue = string.Empty;

        //    foreach (GridViewRow gvr in GridViewCus1.Rows)
        //    {
        //        string currentValue = gvr.Cells[sortColumnIndex].Text;
        //        if (lastValue.CompareTo(currentValue) != 0)
        //        {
        //            int rowindex = gridTable.Rows.GetRowIndex(gvr);
        //            // Add a new sort header row
        //            GridViewRow sortRow = new GridViewRow(rowindex, rowindex,
        //                DataControlRowType.DataRow, DataControlRowState.Normal);
        //            TableCell sortCell = new TableCell();
        //            sortCell.ColumnSpan = GridViewCus1.Columns.Count;
        //            sortCell.Text = string.Format("{0}: {1}",
        //                sortColumnHeaderText, currentValue);
        //            sortCell.CssClass = "SortHeaderRowStyle";
        //            // Add sortCell to sortRow, and sortRow to gridTable
        //            sortRow.Cells.Add(sortCell);
        //            gridTable.Controls.AddAt(rowindex, sortRow);
        //            // Update lastValue
        //            lastValue = currentValue;
        //        }
        //    }
        //}
        base.Render(writer);
    }
    protected void GridViewCus1_Sorting(object sender, GridViewSortEventArgs e)
    {
        
    }
    protected void lnkbtnRestPage_Click(object sender, EventArgs e)
    {
        this.ddlEcsInternalOffice.SelectedIndex = -1;
        this.TextBoxSearchCity.Text = string.Empty;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
        "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridViewCus1.AllowPaging = false;
        GridViewCus1.DataBind();



        GridViewCus1.RenderControl(hw);
        
        
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
        //Response.Clear();
        //Response.Buffer = true;

        //Response.AddHeader("content-disposition",
        //"attachment;filename=GridViewExport.xls");
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //Response.Charset = "utf-8";
        //Response.ContentType = "application/vnd.ms-excel";
        
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter hw = new HtmlTextWriter(sw);
        //HtmlForm frm = new HtmlForm();
        //GridViewCus1.AllowPaging = false;
        //GridViewCus1.DataBind();

        //GridViewCus1.Parent.Controls.Add(frm);

        //frm.Attributes["runat"] = "server";
        //frm.Controls.Add(GridViewCus1);
        //frm.RenderControl(hw);

    
        //Response.Output.Write(sw.ToString());
        //Response.Flush();
        //Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
}
